//
//  RanIntroduceViewController.m
//  test
//
//  Created by Ran on 15/10/29.
//  Copyright (c) 2015年 gintong. All rights reserved.
//

#import "RanIntroduceViewController.h"

#import "RanIntroduceAnimationPageOne.h"
#import "RanIntroduceAnimationPageTwo.h"
#import "RanIntroduceAnimationPageThree.h"
#import "RanIntroduceAnimationPageFour.h"
#import "RanIntroduceAnimationPageFive.h"

@interface RanIntroduceViewController ()<UIScrollViewDelegate>

@property(nonatomic, weak)UIScrollView *scrollView;
@property(nonatomic, strong)NSArray *dataSource;
@property(nonatomic, weak)UIPageControl *pageControl;

@end

@implementation RanIntroduceViewController

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupParams];
    [self setupUI];
    [self showAnimationAtIndex:0];
}

- (void)dealloc
{
    NSLog(@"dealloc");
}

#pragma mark - Private

- (void)setupParams
{
    
}

- (void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.dataSource.count, self.view.frame.size.height);
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    for (NSInteger i = 0; i < self.dataSource.count; i++) {
        UIView *animationView = self.dataSource[i];
        CGRect frame = animationView.frame;
        frame.origin.x = scrollView.frame.size.width * i;
        animationView.frame = frame;
        [scrollView addSubview:animationView];
    }
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 120 * kPercent, 44 * kPercent)];
    pageControl.center = CGPointMake(kScreenWidth / 2, kScreenHeight - 44 * kPercent);
    pageControl.backgroundColor = [UIColor clearColor];
    pageControl.numberOfPages = self.dataSource.count;
    pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:1 alpha:0.3];
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

#pragma mark - Delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [self showAnimationAtIndex:index];
    [self removeAnimationAtIndex:index - 1];
    [self removeAnimationAtIndex:index + 1];
    self.pageControl.currentPage = index;
}

- (void)showAnimationAtIndex: (NSInteger)index
{
    if (index >= 0 && index < self.dataSource.count) {
        id<RanAnimationProtocol> animationView = self.dataSource[index];
        [animationView showAnimations];
    }
}

- (void)removeAnimationAtIndex: (NSInteger)index
{
    if (index >= 0 && index < self.dataSource.count) {
        id<RanAnimationProtocol> animationView = self.dataSource[index];
        [animationView reset];
    }
}

#pragma mark - Getter

- (NSArray *)dataSource
{
    if (!_dataSource) {
        RanIntroduceAnimationPageFive *pageFive = [[RanIntroduceAnimationPageFive alloc] initWithFrame:self.view.bounds];
        __weak typeof(self) weakSelf = self;
        pageFive.startBtnClickedResponse = ^(UIButton *btn){
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        };
        self.dataSource = @[
                            [[RanIntroduceAnimationPageOne alloc] initWithFrame:self.view.bounds],
                            [[RanIntroduceAnimationPageTwo alloc] initWithFrame:self.view.bounds],
                            [[RanIntroduceAnimationPageThree alloc] initWithFrame:self.view.bounds],
                            [[RanIntroduceAnimationPageFour alloc] initWithFrame:self.view.bounds],
                            pageFive
                            ];
    }
    return _dataSource;
}

@end
